//
//  CycleImage.m
//  轮播图封装
//
//  Created by 杨智超 on 15/11/6.
//  Copyright © 2015年 杨智超. All rights reserved.
//

#import "CycleImage.h"
#import "UIImageView+WebCache.h"
@interface CycleImage ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, assign)int indx;

@end
@implementation CycleImage

+ (instancetype)CycleViewWithArray:(NSArray *)array frame:(CGRect)frame page:(CGRect)pageFrame
{
    CycleImage *cycle = [[CycleImage alloc]initWithFrame:frame];
    [cycle CycleViewWithArray:array frame:frame page:pageFrame];
    
    return [cycle autorelease];
}

- (void)CycleViewWithArray:(NSArray *)array frame:(CGRect)frame page:(CGRect)pageFrame;
{
    self.scroll = [[UIScrollView alloc]initWithFrame:frame];
    self.scroll.backgroundColor = [UIColor lightGrayColor];
    self.scroll.contentSize = CGSizeMake(frame.size.width * (array.count + 2), 0);
            /*添加手势*/
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self.scroll addGestureRecognizer:tap];
        [tap release];
    [self addSubview:self.scroll];
    [self.scroll release];
    UIImageView *image0 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,frame.size.width,frame.size.height)];
    [image0 sd_setImageWithURL:[NSURL URLWithString:[array lastObject]]];
    [self.scroll addSubview:image0];
    [image0 release];
    
    
    for (int i = 0; i < array.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * (i + 1), 0,frame.size.width,frame.size.height)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[array objectAtIndex:i]]];
        [self.scroll addSubview:imageView];
        [imageView release];
        
    }
    
    UIImageView *imagela = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * (array.count + 1) , 0,frame.size.width,frame.size.height)];
    [imagela sd_setImageWithURL:[NSURL URLWithString:[array objectAtIndex:0]]];
    [self.scroll addSubview:imagela];
    [self addSubview:self.scroll];
    
    
    self.scroll.pagingEnabled = YES;
    self.scroll.bounces = NO;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.contentOffset = CGPointMake(frame.size.width, 0);
    self.scroll.delegate = self;
    self.page = [[UIPageControl alloc] initWithFrame:pageFrame];
    self.page.alpha = 1;
    self.page.numberOfPages = array.count;
    /* 标志所在位置的颜色 */
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    /* 不是标志所在位置的颜色 */
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    [self addSubview:self.page];
    [self.page release];
    self.page.currentPage = 0;
    
    
    /*添加定时器*/
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(hangle) userInfo:nil repeats:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.page.currentPage = self.scroll.contentOffset.x / self.scroll.frame.size.width - 1;
    if (self.scroll.contentOffset.x <= 0) {
        
        self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width * (self.scroll.contentSize.width / self.scroll.frame.size.width - 2), 0);
    } else if (self.scroll.contentOffset.x >= self.scroll.frame.size.width * (self.scroll.contentSize.width / self.scroll.frame.size.width - 1)) {
        
        self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width, 0);
    }
}
/*定时器方法*/
- (void)hangle{
    self.indx++;
    self.page.currentPage = self.indx - 1;
    self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width * self.indx, 0);
    if (self.scroll.contentOffset.x >=self.scroll.frame.size.width * (self.scroll.contentSize.width / self.scroll.frame.size.width - 1)) {
        self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width, 0);
        self.indx = 1;
        self.page.currentPage = self.indx - 1;
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    if ([self.deleagte respondsToSelector:@selector(didClick)]) {
        [self.deleagte didClick];
     
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
